import { Component, OnInit } from '@angular/core';
import { MrpManageService } from '../mrp-manage.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { HCommonService } from 'src/app/h-common/h-common.service';

@Component({
    selector: 'app-mrp-detail',
    templateUrl: './mrp-detail.component.html',
    styleUrls: ['./mrp-detail.component.scss']
})
export class MrpDetailComponent implements OnInit {
    private parentId: number = -1;
    listData = [];
    mrpHouse = [];
    selectMrpData = [];
    mrpAreaData: [] = [];
    isShow: boolean = false;
    selectItem: { id?: number } = {};
    detailForm: FormGroup;
    isLoading: boolean = false;
    isDelete: boolean = false;
    bornRole = {}

    constructor(
        private mrpService: MrpManageService,
        private fb: FormBuilder,
        private commonService : HCommonService
    ) { }

    ngOnInit() {
        this.bornRole = this.commonService.getRoles("erpWarehouse")

        this.detailForm = this.fb.group({
            warehouseId: [null, Validators.required]
        })
    }

    ngDoCheck() {
        if (this.parentId !== this.mrpService.detailData.id) {
            let _res = this.mrpService.detailData
            this.parentId = _res.id;
            this.listData = _res.erpWarehouseAreas;
            this.mrpHouse = _res.warehouses;
        }
    }
    showModal(data) {
        this.initModal(data);
        this.selectMrpData = this.mrpHouse;
        this.isDelete = false;
    }
    handleCancel() {
        this.isShow = false;
    }
    submitForm() {
        let _dataJson = {
            erpWarehouseId: this.selectItem.id,
            warehouseId: this.detailForm.controls["warehouseId"].value
        }
        this.isLoading = true;
        if (this.isDelete) {
            this.mrpService.apiAreaDelete(_dataJson).subscribe(res => {
                this.frontDataChange({
                    id: this.detailForm.controls["warehouseId"].value
                }, 2);
                this.isShow = false;
                this.isLoading = false;
            }, () => {
                this.isLoading = false;
            })
        } else {
            this.mrpService.apiAreaSave(_dataJson).subscribe(res => {
                let _id = this.detailForm.controls["warehouseId"].value;
                this.frontDataChange({
                    name: this.filterMrpAreaItem(_id).name,
                    id: _id
                }, 1);
                this.isShow = false;
                this.isLoading = false;
            }, () => {
                this.isLoading = false;
            });
        }
    }
    delete(data) {
        this.initModal(data);
        this.selectMrpData = [{
            name: "MRP关联库区",
            warehouseAreas: data.warehouseAreas
        }];
        this.isDelete = true;
    }
    initModal(data){
        this.isShow = true;
        this.detailForm.reset();
        this.selectItem = data;
    }
    filterMrpAreaItem(id) {
        let _item = {
            name: ""
        };
        this.mrpHouse.forEach(item => {
            item.warehouseAreas.forEach($item => {
                if ($item.id === id) {
                    _item = $item
                }
            })
        })
        return _item
    }
    frontDataChange(data, type) {
        let _item = this.listData.filter(item => {
            return item.id == this.selectItem.id
        })[0]

        if (type == 1) {    // 添加
            let _r = _item.warehouseAreas.some(item=>{
                return item.id === data.id
            })
            if( !_r ){
                _item.warehouseAreas.push(data);
            }
        }
        if (type == 2) {    // 删除
            _item.warehouseAreas.forEach((item, index) => {
                if (item.id === data.id) {
                    _item.warehouseAreas.splice(index, 1)
                }
            });
        }
    }
}
